package com.yycx.module.bus.provider.handler.haiying.settle.deptsettle;

import com.yycx.common.base.entity.EntityMap;
import com.yycx.common.base.utils.FlymeUtils;
import com.yycx.common.constants.CommonConstants;
import com.yycx.common.mybatis.interceptor.SaveInterceptor;
import com.yycx.common.mybatis.query.CriteriaSave;
import com.yycx.common.security.OpenHelper;
import com.yycx.common.utils.DateUtils;
import com.yycx.common.utils.RedisUtils;
import com.yycx.module.bus.client.entity.BusDept;
import com.yycx.module.bus.client.entity.BusPayment;
import com.yycx.module.bus.client.entity.BusSaleman;
import com.yycx.module.bus.client.enums.AmountTypeEnum;
import com.yycx.module.bus.provider.handler.haiying.BusinessUtils;
import com.yycx.module.bus.provider.service.BusDeptService;
import com.yycx.module.bus.provider.service.BusSalemanService;
import com.yycx.module.bus.provider.service.hy.BusHyorderReturnService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.math.BigDecimal;

/**
 * 部门结算单保存扩展器
 *
 * @author zyf
 */
@Component
public class HyDeptSettleApplyPaymentSaveHandler implements SaveInterceptor<BusPayment> {

    @Resource
    private BusHyorderReturnService orderDetailsService;

    @Resource
    public RedisUtils redisUtils;

    @Autowired
    private BusinessUtils businessUtils;


    @Autowired
    private BusSalemanService salemanService;
    @Autowired
    private BusDeptService deptService;

    @Override
    public void prepare(CriteriaSave cs, EntityMap entityMap, BusPayment payment) {
        //结算数据
        String selectIds = entityMap.get("selectIds");
        EntityMap totalSettleMap = orderDetailsService.totalDeptSettleApplyAmount(selectIds);
        if (FlymeUtils.isNotEmpty(totalSettleMap)) {
            Long deptId = totalSettleMap.getLong("deptId");
            String drugType = totalSettleMap.get("drugType");
            if (FlymeUtils.isNotEmpty(deptId)) {
                BusDept dept = deptService.getById(deptId);
                if (FlymeUtils.isNotEmpty(dept)) {
                    payment.setDeptName(dept.getDeptName());
                }
            }
            payment.setDrugType(drugType);
            Long saleManId = totalSettleMap.getLong("saleManId");
            BusSaleman saleman = salemanService.getById(saleManId);
            if (FlymeUtils.isNotEmpty(saleman)) {
                payment.setSaleManId(saleManId);
                payment.setPayeeName(saleman.getSaleManName());
                payment.setSaleManCode(saleman.getSaleManBusinessNo());
            }
            BigDecimal deptSettleAmount = totalSettleMap.getBigDecimal("deptSettleAmount");
            payment.setAmount(deptSettleAmount);
        }
        Long companyId = OpenHelper.getCompanyId();
        Long userId = OpenHelper.getUserId();
        payment.setTitle("部门费用结算");
        payment.setAmountDate(DateUtils.getNowDate());
        payment.setOrganizationId(companyId);
        payment.setCompanyId(companyId);
        payment.setPrintTag(CommonConstants.INT_0);
        payment.setUserId(OpenHelper.getUserId());
        payment.setNickName(OpenHelper.getNickName());
        payment.setSettleType(0);
        payment.setAmountType(AmountTypeEnum.DeptSettle.getCode());
        String paymentNo = businessUtils.getPaymentNo(companyId, userId, AmountTypeEnum.DeptSettle);
        payment.setPaymentNo(paymentNo);
        payment.setPaymentState(CommonConstants.INT_0);
        payment.setSelectIds(selectIds);
    }

    @Override
    public void complete(CriteriaSave cs, EntityMap params, BusPayment entity) {
        String settleIds = cs.getParams("selectIds");
        if (FlymeUtils.isNotEmpty(settleIds)) {
            //关联部门结算申请ID
            orderDetailsService.updateDeptSettleApplyId(entity.getPaymentId(), settleIds);
        }

    }
}
